package com.fengpanlong.web;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fengpanlong.entity.Employee;
import com.fengpanlong.entity.EmployeeProject;
import com.fengpanlong.entity.Message;
import com.fengpanlong.entity.Project;
import com.fengpanlong.entity.Task;
import com.fengpanlong.service.EmployeeService;
import com.fengpanlong.service.GogalService;
import com.fengpanlong.service.MessageService;
import com.fengpanlong.service.ProjectService;
import com.google.gson.Gson;

public class ProjectServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getMethod();
		String m = request.getParameter("_m");
		if("GET".equalsIgnoreCase(method)) {
			if("newProject".equals(m)) {
				newProject(request,response);
			} else if("getAddMsg".equals(m)) {
				getAddMsg(request,response);
			} else {
				showProject(request,response);
			}
 		} else {
 			if("saveProject".equals(m)) {
				saveProject(request,response);
			} else if("reportMsg".equals(m)) {
				reportMsg(request,response);
			}
		}
	}
	/**
	 * 获得增加的消息
	 * @param request
	 * @param response
	 */
	private void getAddMsg(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int num = Integer.valueOf(request.getParameter("num"));
		List<Message> messageList = ProjectService.getAddMsg(num);
		response.setContentType("application/json;charset=UTF-8");
		Gson gson = new Gson();
		String json = gson.toJson(messageList);
		PrintWriter out = response.getWriter();
		out.print(json);
		out.flush();
		out.close();
	}
	/**
	 * 发表消息
	 * @param request
	 * @param response
	 */
	private void reportMsg(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String content = request.getParameter("content");
		content = "说：" + content;
		Message message = MessageService.sendMessage(request,content);
		
		response.setContentType("application/json;charset=UTF-8");
		Gson gson = new Gson();
		String json = gson.toJson(message);
		PrintWriter out = response.getWriter();
		out.print(json);
		out.flush();
		out.close();
	}
	/**
	 * 去project.jsp页面
	 * @param request
	 * @param response
	 */
	private void showProject(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String projectId = request.getParameter("id");
		Employee employee = (Employee)request.getSession().getAttribute("employee");
		EmployeeProject ep = ProjectService.findEPByEmployeeIdAndProjectId(employee.getId(),projectId );
		if(ep == null ) {
			response.sendError(403);
			return;
		}
		request.getSession().setAttribute("ep", ep);
		List<Task> taskList = GogalService.findTasksByEmployeeIdAndByProjectId(ep.getEmployeeId(), ep.getProjectId());
		request.setAttribute("taskList", taskList);
		List<Message> messageList = MessageService.findMessagesByProjectId(projectId);
		request.setAttribute("messageList", messageList);
		goTojsp(request, response,"project.jsp");
	}
	/**
	 * 保存项目
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void saveProject(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String title = request.getParameter("title");
		String desc = request.getParameter("desc");
		String manager = request.getParameter("manager");
		String employees = request.getParameter("employees");
		try {
			ProjectService.saveNewProject(title,desc,manager,employees);
		} catch (Exception e) {
			String msg = e.getMessage();
			request.setAttribute("msg", msg);
			List<Employee> employeeList = EmployeeService.findEmployeesForList();
			request.setAttribute("employeeList", employeeList);
			goTojsp(request, response, "newProject.jsp");
			return;
		} 
		Project project = ProjectService.findProjectByName(title);
		request.setAttribute("project",project);
		String content ="创建了名字为" + "<a href = \"project.jspx?id=" + project.getId() + "\">" + project.getName() + "</a>的项目。";
		MessageService.sendMessage(request,content,project.getCreatetime());
		response.sendRedirect("main.jspx");
	}
	/**
	 * 新建项目
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void newProject(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<Employee> employeeList = EmployeeService.findEmployeesForList();
		request.setAttribute("employeeList", employeeList);
		goTojsp(request, response, "newProject.jsp");
	}
	/**
	 * 去jsp页面
	 * @param request
	 * @param response
	 * @param jspName
	 * @throws ServletException
	 * @throws IOException
	 */
	private void goTojsp(HttpServletRequest request,
			HttpServletResponse response,String jspName) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/views/" + jspName).forward(request, response);		
	}
}
